package zl.ordermanager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zl.ordermanager.dao.RepairDao;
import zl.ordermanager.po.Trepair;
import zl.ordermanager.service.RepairService;
import zl.ordermanager.utils.ConfigUtil;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.Repair;
import zl.ordermanager.vo.SessionInfo;

@Service
public class RepairServiceImpl implements RepairService {

	@Autowired
	private RepairDao repairDao;

	@Override
	public DataGrid<Repair> dataGrid(HttpSession session, PageHelper pageHelper) {
		DataGrid<Repair> dataGrid = new DataGrid<Repair>();
		List<Repair> repairList = new ArrayList<Repair>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			params.put("keyword", "%%" + pageHelper.getKeywords() + "%%");
		}
		String hql = " from Trepair t ";
		String whereHql = whereHql(session, pageHelper);
		List<Trepair> trepairList = repairDao.find(hql + whereHql + orderHql(pageHelper), params,
				pageHelper.getPageNum(), pageHelper.getNumPerPage());
		if (trepairList != null && trepairList.size() > 0) {
			for (Trepair trepair : trepairList) {
				Repair repair = new Repair();
				BeanUtils.copyProperties(trepair, repair);
				repairList.add(repair);
			}
		}
		dataGrid.setList(repairList);
		dataGrid.setTotal(repairDao.count("select count(*) " + hql + whereHql, params));
		return dataGrid;
	}

	private String whereHql(HttpSession session, PageHelper pageHelper) {
		String whereString = "";
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			whereString += " where t.product like :keyword ";
			whereString += " or t.takeFromCustomer like :keyword ";
			whereString += " or t.repairDatetime like :keyword ";
			whereString += " or t.takeBackDatetime like :keyword ";
			whereString += " or t.remarks like :keyword ";
		}

		String userFilter = "";
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		if (sessionInfo != null) {
			if (sessionInfo.getRole().equals("业务员")) {
				userFilter = " (t.salesman = '" + sessionInfo.getRealname() + "')";
			}
		}

		if (!userFilter.isEmpty()) {
			if (!whereString.isEmpty()) {
				whereString = whereString + " and " + userFilter;
			} else {
				whereString = "where " + userFilter;
			}
		}

		return whereString;
	}

	private String orderHql(PageHelper pageHelper) {
		String orderString = "";
		if (pageHelper.getOrderField() != null && !pageHelper.getOrderField().isEmpty()
				&& pageHelper.getOrderDirection() != null && !pageHelper.getOrderDirection().isEmpty()) {
			orderString = " order by t." + pageHelper.getOrderField() + " " + pageHelper.getOrderDirection();
		}
		return orderString;
	}

	@Override
	public Repair get(Integer id) {
		Trepair trepair = repairDao.get(Trepair.class, id);
		if (trepair == null) {
			return null;
		}
		Repair repair = new Repair();
		BeanUtils.copyProperties(trepair, repair);
		return repair;
	}

	@Override
	public void add(Repair repair) throws Exception {
		Trepair trepair = new Trepair();
		BeanUtils.copyProperties(repair, trepair);
		repairDao.save(trepair);
	}

	@Override
	public void edit(Repair repair) throws Exception {
		Trepair trepair = repairDao.get(Trepair.class, repair.getId());
		BeanUtils.copyProperties(repair, trepair);
	}

	@Override
	public void delete(Integer id) {
		repairDao.delete(repairDao.get(Trepair.class, id));
	}

}
